Gosub
ChoiceScript supports a simple form of subroutines. A subroutine is a section of code able to be accessed (using the *gosub command) where & when required at many different points within the same scene file. This essentially negates the need to copy-paste an identical piece of code over & over to use in different places, and also makes any later changes much quicker to implement. Note that the *gosub command only works within the same scene file. An extension of this command, *gosub_scene, allows a subroutine to be called from any scene file, further reducing the duplication of identical code accross the game files. Usage For example, let's assume that in your game you have four main stats (say, Strength, Dexterity, Intelligence & Wisdom) shown on your Stats page as percentile bars, which means you probably don't want them to ever exceed a value of 100. To make certain of this you can use a very simple subroutine, which you would probably place down at the bottom of your scene file. It might look something like this: *label cap_stats *if strength > 100 *set strength 100 *if dexterity > 100 *set dexterity 100 *if intelligence > 100 *set intelligence 100 *if wisdom > 100 *set wisdom 100 *return You have now effectively created a subroutine with a label of 'cap_stats', by the simple expedient of adding that vital *return command at the end. The *return command is only ever used to signify a subroutine. You can now use this subroutine's ready-made scripting anywhere within the same file (e.g. wherever you apply any new stats gains), without having to repeat or copy-paste those lines anywhere else in the scene, simply by using the *gosub command where required. For example: *choice #Train body. *set strength +10 *set dexterity +10 *gosub cap_stats You train your body. *finish #Train mind. *set intelligence +10 *set wisdom +10 *gosub cap_stats You sharpen your mind. *finish When the game reaches a *gosub cap_stats line it will immediately jump to the *label cap_stats line (as shown in the previous example) and execute all the scripting there, until it hits a *return command, at which point it will immediately jump back to the originating *gosub line and continue on from that point. In essence, where you use that particular *gosub cap_stats, ChoiceScript will effectively do this: *choice #Train body. *set strength +10 *set dexterity +10 *if strength > 100 *set strength 100 *if dexterity > 100 *set dexterity 100 *if intelligence > 100 *set intelligence 100 *if wisdom > 100 *set wisdom 100 You train your body. *finish #Train mind. *set intelligence +10 *set wisdom +10 *if strength > 100 *set strength 100 *if dexterity > 100 *set dexterity 100 *if intelligence > 100 *set intelligence 100 *if wisdom > 100 *set wisdom 100 You sharpen your mind. *finish This is just a simple example to illustrate how a *gosub--''run subroutine script''--*return combination works. However, there is no real limit to how long or complex a single subroutine can be, making it one of the most useful ChoiceScript features. Tip: If you find yourself repeating a particular section of code several times, consider if it might not be best turning it into a subroutine and just using *gosub within your actual story scripting - especially if there's a chance you may later wish to modify any part of that code. It's much easier & quicker to edit it when it's all in a single subroutine near the bottom, rather than when repeated several times throughout the file. Advanced *gosub Note that subroutines can be nested, meaning that within one subroutine you can *gosub to another, and then another, and so on, until it hits an actual *return command. By this means it's possible to create quite intricate & complicated programming. However, a typical ChoiceScript game would probably never need to do so. For most games it should suffice to use multiple simple subroutines for entirely different, and completely unrelated, purposes. Note Although the example above has been used to illustrate how, and under what sort of circumstances, you should consider using a *gosub subroutine, the easiest way to avoid problems with uncapped percentile values is to use ChoiceScript's "Fairmath" system -- covered in more detail in the article on Arithmetic Operators. Category:Commands